/***************************************************************************
    begin                : aug 01 2010
    copyright            : (C) 2010 by Benoit Valot
    email                : benvalot@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 23 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

package val.lemonde;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.util.Log;

public class PageLoad {

	private String content;

	private URL url;
	
	private String urlString;

	public PageLoad(String u) throws Exception {
		setContent("");
		u.replaceAll(" ", "");
		urlString = u;
	}

	private String getPage() throws Exception {
		StringBuffer sb = new StringBuffer("");
		BufferedReader in = null;
		try {
			//on charge l'artcile
			url = new URL(urlString);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			in = new BufferedReader(
					new InputStreamReader(conn.getInputStream(),"UTF-8")); //ISO-8859-1

			String ligneCodeHTML;
			boolean data = false;

			while ((ligneCodeHTML = in.readLine()) != null) {
				ligneCodeHTML = " " + ligneCodeHTML;
				//nouveau code
				if (ligneCodeHTML.contains("<article class=\"article")){
					ligneCodeHTML="<body>"+"\n"+"<article class=\"article\">"+"\n";
					data = true;
					Log.d("lemonde","start article");
				}
				//nouveau code
				if (ligneCodeHTML.contains("</article>")){
					data = false;
					sb.append("</article>"+"\n"+"</body>");
					Log.d("lemonde","end article");
				}
				//pour le portfolio
				if (ligneCodeHTML.contains("<div id=\"footer\""))
					data = false;

				if (data) {
					//on enleve les tailles pour les images et autres
					//ligneCodeHTML = ligneCodeHTML.replaceFirst("width=\".*?\"", "width=\"100%\"");
					ligneCodeHTML = ligneCodeHTML.replaceFirst("height=\".*?\"", "");
					sb.append(ligneCodeHTML);
					//sb.append("\n");
				}
			}
			in.close();
			
			if (sb.toString().equalsIgnoreCase(""))
				throw new StopException("Page vide");
			
			//sb.append("<hr />");
			
		} catch (java.net.ProtocolException e) {
			throw new StopException("Problème de connection");
		} catch (Exception e) {
			throw e;
		} finally {
			// Dans tous les cas on ferme le bufferedReader s'il n'est pas null
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
				}
			}
		}
		
		return this.get_style() + sb.toString();
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getContent() throws Exception {
		content = this.getPage();
		return content;
	}

	public void setUrl(URL url) {
		this.url = url;
	}

	public URL getUrl() {
		return url;
	}

	public String center(String S) {
		String S2 = "<p style=\"text-align: center;\">";
		S2 = S2 + S + "</p>";
		return (S2);
	}

	public String get_style() {
		StringBuffer sb2 = new StringBuffer("");
		sb2.append("<style type=\"text/css\">");
		sb2.append(new CssStyle().get_css_data());
		sb2.append("</style>");
		return (sb2.toString());
	}

}
